package com.gkd.controller;

import com.github.pagehelper.PageInfo;
import com.gkd.dao.DepartmentDao;
import com.gkd.dao.EmployeeDao;
import com.gkd.pojo.Department;
import com.gkd.pojo.Employee;
import com.gkd.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
// 将会话属性selectedIds绑定到模型
//@SessionAttributes("selectedIds")//一写这方法之间无法传session
public class EmployeeController {
    //实例化dao
    @Autowired
    public EmployeeDao employeeDao;
    //实例化部门dao
    @Autowired
    public DepartmentDao departmentDao;
    // 假设你有一个服务来处理员工数据
    @Autowired
    private EmployeeService employeeService;

    // 假设这是显示员工列表的GET请求处理方法
    //分页查询部分员工信息
    Map<Integer,Boolean> kyibm = new HashMap<>();//初始化
    List<Integer> bianli = new ArrayList<>();
    @RequestMapping("/emps")
    public String queryAll2(HttpServletRequest request, Model model, HttpSession session,@RequestParam(value = "cbv", required = false) Integer[] cbValues,@RequestParam(value = "clickedButton", required = false) Integer Btv){
        Integer a1=1;
        Integer a2=2;
        if(cbValues!=null){
                for (int i=0;i<bianli.size();i++){
                    int ifture = 0;
                    Integer valueposition = -1;
                    for (Integer value : cbValues) {
                        if (bianli.get(i)==value){
                            kyibm.put(value,true);
                            ifture = 1;
                        }
                        valueposition=value;
                    }
                    if(ifture==0){
                        kyibm.put(bianli.get(i),false);
                        kyibm.put(valueposition,true);
                    }
                }
        }
        if(Btv!=null&&Btv==0){
            if (cbValues==null){
                for (int i =0;i<bianli.size();i++){
                    kyibm.put(bianli.get(i),false);
                }
            }
        }
        bianli.clear();
        if(kyibm.size()==0){
            kyibm=employeeDao.reback();
//            List<Integer> eid=employeeDao.queryEmployeeAllId();
//            for (int i=0;i<employeeDao.sumAllIdNum();i++){
//                kyibm.put(eid.get(i),false);
//            }
        }
        if(a1==Btv){
            List<Integer> eid=employeeDao.queryEmployeeAllId();
            for (int i=0;i<employeeDao.sumAllIdNum();i++){
                kyibm.put(eid.get(i),!kyibm.get(eid.get(i)));
            }
        }
        else if(a2==Btv){
            List<Integer> eid=employeeDao.queryEmployeeAllId();
            for (int i=0;i<employeeDao.sumAllIdNum();i++){
                kyibm.put(eid.get(i),true);
            }
        }
        Map<Integer,Department> ym = departmentDao.departmentMap();
        Integer pageNum1 = request.getParameter("pageNum")==null?1:Integer.valueOf(request.getParameter("pageNum"));
        PageInfo<Employee> pageInf = employeeService.queryAllPages(pageNum1, 4);
        for (int i = 0;i<pageInf.getList().size();i++){
            pageInf.getList().get(i).setDepartment(ym.get(pageInf.getList().get(i).getDe_id()));
        }
        for(int i =0;i<pageInf.getList().size();i++){
            bianli.add(pageInf.getList().get(i).getId());
        }
        employeeDao.setCheckListByIdMap(kyibm);
        System.out.println("得到："+employeeDao.getCheckListByIdMap());
        model.addAttribute("ibm",kyibm);
        model.addAttribute("emps",pageInf.getList());
        model.addAttribute("pages",pageInf.getPages());
        model.addAttribute("local_pageNum",pageInf.getPageNum());
        model.addAttribute("pageSize",pageInf.getSize());
        model.addAttribute("hasPreviousPage",pageInf.isHasPreviousPage());
        model.addAttribute("hasNextPage",pageInf.isHasNextPage());

        return "emp/list"; //返回 user字符串? yemian zhujiejueding C shitu RC zifuchuang
    }


    //查询全部员工信息
//    @RequestMapping("/emps")
//    public String list(Model model){
//        Collection<Employee> employees = employeeDao.getAll();
//        System.out.println(employees);
//        //通过model把员工信息返回给前端
//        model.addAttribute("emps",employees);
//        return "emp/list";
//    }
    //增加员工  1跳转到添加页面
    @GetMapping("/emp")
    public String toAddPage(Model model){
        //先要查询出所有部门
        Collection<Department> department = departmentDao.getDepartment();
        model.addAttribute("departments",department);
        return "emp/add";//返回的是视图，也就是添加页面
    }
    //添加第二步，执行添加，然后查询
    @PostMapping("/emp")
    public String addEmp(Employee employee){
        employeeDao.save(employee);
        return "redirect:/emps";//这里执行什么操作？  视图 ？ API ？
        //这里重定向相当于执行了刷新页面操作
    }

    //修改 分成两步： 1.跳转页面 2.保存+查询
    //修改第一步
    @GetMapping("/emp/{id}")
    public String toUpdateEmp(@PathVariable("id") Integer id,Model model){
        //查出原来的员工信息
        if(employeeDao.fem==null){
            employeeDao.EmployeeMap();
        }
        Employee employee = employeeDao.getEmployeeById(id);
        model.addAttribute("emp",employee);
        //先要查询出所有部门
        Collection<Department> department = departmentDao.getDepartment();
        model.addAttribute("departments",department);
        return "emp/update";//跳转修改视图
    }

    @PostMapping("/updateEmp")
    //修改第二步  保存修改后的用户信息
    public String updateEmp(Employee employee){
        employeeDao.update(employee);
        return "redirect:/emps";
    }

    //删除
    @GetMapping("/delemp/{id}")
    public String deleteEmp(@PathVariable("id") Integer id){
        employeeDao.delete(id);
        return "redirect:/emps";//又进行了一次查询
    }
}
